<?php

namespace app\index\diyclass;
use app\index\model\Task as TaskModal;

class Task
{
    private $userid = null;
    private $key = '';
    public function __construct($userid, $key)
    {
      $this->userid = $userid;
      $this->key = $key;
    }
    private function getInterNum()
    {
        $num = 0;
        switch ($this->key) {
            case "COMMENT_TASK":
                $num = 10;
                break;
            case "ADD_TAG_TASK":
                $num = 40;
                break;
        }
        return $num;
    }
    private function getTotal()
    {
        $total = 0;
        switch ($this->key) {
            case "COMMENT_TASK":
                $total = 5;
                break;
            case "ADD_TAG_TASK":
                $total = 10;
                break;
        }
        return $total;
    }
    private function getTitle()
    {
        $title = '未知任务';
        switch ($this->key) {
            case "COMMENT_TASK":
                $title ='发表'.$this->getTotal().'条评论';
                break;
            case "ADD_TAG_TASK":
                $title ='设置'.$this->getTotal().'次标签';
                break;
        }
        return $title;
    }
    private function getDesc()
    {
        $title = '未知奖励';
        switch ($this->key) {
            case "COMMENT_TASK":
                $title ='奖励'.$this->getInterNum().'硬币';
                break;
            case "ADD_TAG_TASK":
                $title ='奖励'.$this->getInterNum().'硬币';
                break;
        }
        return $title;
    }
    private function getContent()
    {
        $text = '';
        switch ($this->key) {
            case "COMMENT_TASK":
                $text = "完成".$this->getTotal().'次评论';
                break;
            case "ADD_TAG_TASK":
                $text = "完成".$this->getTotal().'次添加条钱';
                break;
        }
        return $text;
    }
    public function setKey($key)
    {
        $this->key = $key;
    }

    public function getTaskInfo()
    {
        $sql = new TaskModal();
        $whereArr[] = ['user_id',  '=', $this->userid];
        $whereArr[] = ['type',  '=', $this->key];
        $info = $sql->where($whereArr)->findOrEmpty();
        if(!$info->isEmpty() && isToday($info['last_time'])) {
            return [
                'key' => $this->key,
                'total' => $this->getTotal(),
                'interNum' => $this->getInterNum(),
                'curren' => $info->count,
                'title' => $this->getTitle(),
                'desc' => $this->getDesc(),
            ];
        }
        $info->delete();
        return [
            'key' => $this->key,
            'total' => $this->getTotal(),
            'interNum' => $this->getInterNum(),
            'curren' => 0,
            'title' => $this->getTitle(),
            'desc' => $this->getDesc(),
        ];
    }

    public function save() {
        $sql = new TaskModal();
        $whereArr[] = ['user_id',  '=', $this->userid];
        $whereArr[] = ['type',  '=', $this->key];
        $info = $sql->where($whereArr)->findOrEmpty();
        if($info->isEmpty()) {
            $sql->save([
                'type' => $this->key,
                'user_id' => $this->userid,
                'count' => 1,
                'last_time' => time(),
            ]);
            return true;
        }
        $total = $this->getTotal();
        if($info->count == $total) return true;
        if(isToday($info['last_time'])) {
            $info->count = $info->count + 1;
        } else {
            $info->count = 1;
        }
        $signInter = $this->getInterNum();
        $info->last_time = time();
        $info->save();
        if($info->count == $total) {
            $log = new Interal($this->userid,$this->key);
            $log->setNum($signInter);
            $log->setContent($this->getContent());
            $log->save();
        }
        return true;
    }
}